package com.dorso.IOC;

import org.dom4j.Document;
import org.dom4j.DocumentException;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.dorso.Exception.*;

public class MiniApplicationContext {

    Map<String,Object> beans=new HashMap<>();

    public static MiniApplicationContext createIOC(String filename) throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchPropertyException, NoSuchFieldException {
        //1.解析xml文档，得到BeanDefinition的未解析内容
        Document document= XMLLoader.parse(filename);
        List<BeanDefinition> beanDefinitionList=XMLLoader.getBeansElement(document);

        //2.解析BeanDefinition，得到mac
        MiniApplicationContext mac=BeanParser.getMiniApplicationContext(beanDefinitionList);

        return mac;
    }

    public Object getBean(String bean)  throws NoSuchBeanException{
        if(this.beans.containsKey(bean)){
            return this.beans.get(bean);
        }else {
            throw new NoSuchBeanException("不存在"+bean+"这个bean");
        }
    }

}